package com.wffz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wffz.comm.utils.DateUtils;
import com.wffz.config.security.service.IAuthenticationFacade;
import com.wffz.mapper.ShareRecordMapper;
import com.wffz.po.ClientUser;
import com.wffz.po.Patch;
import com.wffz.po.ShareRecord;
import com.wffz.service.ShareRecordService;
import com.wffz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author sunlei
 * @className ShareRecordServiceImpl
 * @Description
 * @date 2023-03-13 20:47
 **/
@Service
@Slf4j
public class ShareRecordServiceImpl implements ShareRecordService {

    @Resource
    private ShareRecordMapper recordMapper;
    @Autowired
    private IAuthenticationFacade authenticationFacade;


    @Autowired
    private UserService userService;

    @Override
    public boolean saveShareRecord(ShareRecord shareRecord) {
        return recordMapper.insert(shareRecord) > 0;
    }

    @Override
    public List<ShareRecord> findTodayMyShareRecord() {
        Authentication authentication = authenticationFacade.getAuthentication();
        log.info(authentication.getName());
        ClientUser user = userService.findUserByUserName(authentication.getName());
        QueryWrapper<ShareRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("invitation_code", user.getRcode());
        wrapper.eq("create_time", DateUtils.getNowDate());
        List<ShareRecord> list = recordMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<ShareRecord> findMyShareRecord() {
        Authentication authentication = authenticationFacade.getAuthentication();
        log.info(authentication.getName());
        ClientUser user = userService.findUserByUserName(authentication.getName());
        QueryWrapper<ShareRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("invitation_code", user.getInvitationCode());
        List<ShareRecord> list = recordMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<ShareRecord> findMyTodayShareRecord() {
        String nowDay = DateUtils.dateTimeNow("yyyy-MM-dd");
        Authentication authentication = authenticationFacade.getAuthentication();
        log.info(authentication.getName());
        ClientUser user = userService.findUserByUserName(authentication.getName());
        QueryWrapper<ShareRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("invitation_code", user.getInvitationCode());
        wrapper.eq("create_day", nowDay);
        List<ShareRecord> list = recordMapper.selectList(wrapper);
        return list;
    }

    @Override
    public int getMyShareCount(long userId) {
        QueryWrapper<ShareRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        int count = recordMapper.selectCount(wrapper);
        return count;
    }
}
